<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2018/7/9
  Time: 15:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>支出管理</title>
    <jsp:include page="/static/common/easyui_header.jsp"/>
    <script>
        function formatterApplyEmp(value, row, index) {
            return value ? value.username : "";
        }
        function formatterBelog(value, row, index) {
            return value ? value.name : "";
        }
        function formatterAuditorEmp(value, row, index) {
            return value ? value.username : "";
        }
        function formatterPayment(value, row, index) {
            console.log(value);
            return value ? value.name : "";
        }
        function formatterStatus(value, row, index) {
            if(value){
                return "<span style='color: green'>已审核</span>"
            }else{
                return " <span style='color: red'>未审核</span>"
            }
        }

        //expensebook_datagrid
        $(function () {

            $('#ss').searchbox({
                searcher:function(value,name){
                    $('#expensebook_datagrid').datagrid('load',{
                        type: name,
                        keyword: value
                    });



                },
                menu:'#mm',
                prompt:'请输入值'
            });


            $('#expensebook_datagrid').datagrid({
                url:'/expenseBook/list',
                fit: true,
                fitColumns: true,
                border: false,
                rownumbers: true,
                pagination: true,
                singleSelect: true,
                toolbar:"#expensebook_linkbutton",
                columns:[[
                    {field:'applyTime',title:'申请时间',width:100},
                    {field:'payMoney',title:'支出金额',width:100},
                    {field:'intro',title:'支出摘要',width:100},
                    {field:'applyEmp',title:'申请人',width:100, formatter: formatterApplyEmp},
                    {field:'payment',title:'支出方式',width:100,formatter: formatterPayment},
                    {field:'spendType',title:'支出类型',width:100},
                    {field:'formNum',title:'单据号',width:100},
                    {field:'belog',title:'所属班级',width:100,formatter: formatterBelog},
                    {field:'auditorEmp',title:'审核人',width:100,formatter: formatterAuditorEmp},
                    {field:'status',title:'状态',width:100,formatter: formatterStatus}

                ]],
                //如果是已经审核状态那么就隐藏审核按钮和编辑按钮
                onSelect:function (idex,data) {
                    if(data.status){
                        //如果是已经审核的已经隐藏
                        $("#changeStatus").linkbutton({
                            text:"",
                            iconCls:"",
                            disable:true
                        })
                        $("#editExpense").linkbutton({
                            text:"",
                            iconCls:"",
                            disable:true
                        })
                        $("#removeExpense").linkbutton({
                            text:"",
                            iconCls:"",
                            disable:true
                        })
                        //禁用按钮
                        $("a[data-cmd='editExpense']").data("cmd", "");
                        $("a[data-cmd='editExpense']").linkbutton("disable");
                        $("a[data-cmd='changeStatus']").data("cmd", "");
                        $("a[data-cmd='changeStatus']").linkbutton("disable");
                        $("a[data-cmd='removeExpense']").data("cmd", "");
                        $("a[data-cmd='removeExpense']").linkbutton("disable");


                    }else{
                        $("#changeStatus").linkbutton({
                            text:"审核",
                            iconCls:"icon-reload",
                            disable:false
                        })
                        $("#editExpense").linkbutton({
                            text:"编辑",
                            iconCls:"icon-edit",
                            disable:false
                        })
                        $("#removeExpense").linkbutton({
                            text:"删除",
                            iconCls:"icon-remove",
                            disable:false
                        })
                    }
                }
            });
            $("#expensebook_add_dialog").dialog({
                title: "添加",
                buttons: '#expensebook_add_linkbutton',
                closed: true

            });
            var cmdObj = {
                //添加申请
                addExpense: function () {
                    //清除表单数据
                    $("#expensebook_add_form").form("clear");
                    //设置标题
                    $("#expensebook_add_dialog").dialog("setTitle", "申请报销");
                    //打开弹窗
                    $("#expensebook_add_dialog").dialog("open");
                },
                reloadExpense:function () {
                    $("#expensebook_datagrid").datagrid("reload")
                },

                //添加班级和编辑班级上的确定按钮
                save: function () {
                    var url="/expenseBook/save";
                    //获取选中行id
                    var classId=$("#class_id").val();
                    console.log(classId);
                    if(classId){
                        url="/expenseBook/update";
                    }
                    $("#expensebook_add_form").form('submit',{
                        url: url,
                        success: function (data) {
                            data=$.parseJSON(data)
                            if(data.success){

                                $.messager.alert("提示","保存成功","info",function () {
                                    //关闭窗口
                                    cmdObj.cancel();
                                    //刷新页面
                                    cmdObj.reloadExpense();
                                })
                            }else{
                                $.messager.alert("提示",data.msg,"error");
                            }
                        }
                    });

                },
                //编辑编辑管理数据
                editExpense:function () {
                    //获取选中行
                    var selectedRow=$("#expensebook_datagrid").datagrid("getSelected")
                    if(!selectedRow){
                        $.messager.alert("提示","请选中一行数据进行操作","error")
                        return ;
                    }
                    //特殊对象数据处理
                    if(selectedRow.belog){
                        selectedRow['belog.id']=selectedRow.belog.id;
                    }
                    if(selectedRow.payment){
                        selectedRow['payment.id']=selectedRow.payment.id;
                    }
                    //读取选中行的数据进行回填
                    $("#expensebook_add_form").form("load",selectedRow);
                    //设置标题
                    $("#expensebook_add_dialog").dialog("setTitle", "审核报销");
                    //打开弹窗
                    $("#expensebook_add_dialog").dialog("open");

                },
                //查询功能
                searchExpense:function () {
                    var beginTime=$("input[name=beginTime]").val();
                    var endTime=$("input[name=endTime]").val();
                    //获取参数

                    //发送查询请求
                    $('#expensebook_datagrid').datagrid('load',{
                        beginTime: beginTime,
                        endTime: endTime
                    });
                },
                //删除功能
                removeExpense:function () {
                    $.messager.alert("提示","给看不给删","error");
                },
                //关闭dialog
                cancel:function () {
                    $("#expensebook_add_dialog").dialog("close");
                },
                //审核功能
                changeStatus:function () {
                    //获取选中行
                    var selectedRow=$("#expensebook_datagrid").datagrid("getSelected")
                    if(!selectedRow){
                        $.messager.alert("提示","请选中一行数据进行操作","error")
                        return ;
                    }else if(selectedRow.status){
                        $.messager.alert("提示","该数据已经通过审核了","error")
                        return;
                    } else{
                        $.messager.confirm("提示","您确定要通过审核吗?",function (yes) {
                            if(yes){
                                //获取选中行发送异步请求
                                $.post("/expenseBook/changeStatus",{id:selectedRow.id},function (data) {
                                    if(data.success){
                                        $.messager.alert("提示","审核成功","info",function () {
                                            //关闭窗口
                                            cmdObj.cancel();
                                            cmdObj.reloadExpense();
                                        })
                                    }
                                })
                            }
                        })
                    }

                },
                //只查看未/已审核
                selectExByStatus:function () {
                  var op = $("#selectExByStatus").linkbutton("options");
                  console.log(op);
                    console.log(op.toggle);
                    if (op.toggle){
                      //如果是真的那么就是产看已审核的
                        $("#selectExByStatus").linkbutton({
                            text:"只查看未审核",
                            iconCls:"",
                            toggle:false
                        })
                        $('#expensebook_datagrid').datagrid('load',{
                            status: 1,
                        });


                    }else{
                        $("#selectExByStatus").linkbutton({
                            text:"只查看已审核",
                            iconCls:"",
                            toggle:true
                        })
                      //查看未审核的
                        $('#expensebook_datagrid').datagrid('load',{
                            status: 0,
                        });

                  }

                }
            }

            //按钮点击事件
            $("a[data-cmd]").click(function () {
                var cmd = $(this).data("cmd");
                cmdObj[cmd]();
            });

        })
    </script>
</head>
<body>
<table id="expensebook_datagrid"></table>
<div id="expensebook_linkbutton">
    <a class="easyui-linkbutton" data-options="iconCls:'icon-add'" plain="true" data-cmd="addExpense">增加</a>
    <a id="removeExpense" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" plain="true" data-cmd="removeExpense">删除</a>
    <a id="editExpense" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" plain="true" data-cmd="editExpense">编辑</a>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-reload'" plain="true" data-cmd="reloadExpense">更新</a>
    <a id="changeStatus" class="easyui-linkbutton" data-options="iconCls:'icon-expend'" plain="true" data-cmd="changeStatus">审核</a>
    <a id="selectExByStatus" class="easyui-linkbutton" data-options="iconCls:'icon-reload'" plain="true" data-cmd="selectExByStatus" toggle="false">只看未审核</a>
    <input id="ss"></input>
    <div id="mm" style="width:300px">
        <div data-options="name:'item.name',iconCls:'icon-ok'">支付方式</div>
        <div data-options="name:'eu.username'">申请人</div>
        <div data-options="name:'cg.name'">班级</div>
        <div data-options="name:'au.username'">审核人</div>
    </div>
    申请时间:
    <input     class= "easyui-datebox" name="beginTime"> </input>~
    <input     class= "easyui-datebox" name="endTime"> </input>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-search'" plain="true" data-cmd="searchExpense">查询</a>
</div>

<div id="expensebook_add_linkbutton">
    <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" plain="true" data-cmd="save">确定</a>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" plain="true" data-cmd="cancel">取消</a>
</div>
<div id="expensebook_add_dialog" style="padding: 10px">
    <form id="expensebook_add_form" method="post">
        <input type="hidden" name="id" id="class_id">
        <table>
            <tr>
                <td>支出金额</td>
                <td><input name="payMoney" class="easyui-textbox"></td>
            </tr>
            <tr>
                <td>支出摘要</td>
                <td><input name="intro" class="easyui-textbox"></td>
            </tr>
            <tr>
                <td>单据号</td>
                <td><input name="formNum" class="easyui-textbox"></td>
            </tr>
            <tr>
                <td>支出类型</td>
                <td><input name="spendType" class="easyui-textbox"></td>
            </tr>

            <tr>
                <td>支出所属</td>
                <td><input id="combobox_belog" class="easyui-combobox" name="belog.id"
                           data-options="valueField:'id',textField:'name',url:'/classgrade/listAllClassGradeJson'"/></td>
            </tr>
            <tr>
                <td>支出方式</td>
                <td><input id="combobox_payment" class="easyui-combobox" name="payment.id"
                           data-options="valueField:'id',textField:'name',url:'/systemDictionaryItem/listAllItemBySn?sn=size'"/></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>
